package com.dql.spring.selector;

import com.dql.spring.bean.Black;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.core.type.AnnotationMetadata;

/**
 * @Author: DQL
 * @Date: 2022/4/29 14:15
 */
public class MyImportBeanDefinitionRegistrar implements ImportBeanDefinitionRegistrar {
    /**
     * @param importingClassMetadata 当前类的注解信息
     * @param registry               BeanDefinition注册类；
     *                               把所有需要添加到容器的bean,调用BeanDefinitionRegistry.registerBeanDefinition手工注册进来
     */
    @Override
    public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
        boolean color = registry.containsBeanDefinition("com.dql.spring.bean.Color");
        boolean yellow = registry.containsBeanDefinition("com.dql.spring.bean.Yellow");
        if (color && yellow) {
            RootBeanDefinition definition = new RootBeanDefinition(Black.class);
            //注册一个bean,指定bean名
            registry.registerBeanDefinition("black", definition);
        }
    }
}
